<template>
  <el-dialog
    :width="'50rem'"
    :visible="dialogVisible"
    title="详情"
    @close="$emit('close')"
  >
    <el-pagination
      style="float: right;"
      layout="prev, pager, next,sizes, total"
      :total="total"
      :page-sizes="[10, 20, 30, 40, 50, 100]"
      :current-page="page+1"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <el-table
      v-loading="loading"
      :data="data"
    >
      <el-table-column
        prop="clusterName"
        label="集群"
      />
      <el-table-column
        prop="cmName"
        label="子集群"
      />
      <el-table-column
        prop="type"
        label="类型"
      >
        <template slot-scope="scope">
          <span 
            style="text-transform:uppercase;"
          >{{ scope.row.type }}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="name"
        label="名称"
      />
      <el-table-column
        prop="healthSummary"
        label="状态"
      >
        <template slot-scope="scope">
          <span>{{ transformState(scope.row.healthSummary) }}</span>
        </template>
      </el-table-column>
    </el-table>
  </el-dialog>
</template>

<script>
import overviewApi from '@/api/monitor/overview'

export default {
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    state: {
      type: Number,
      require: true
    }
  },
  emits: ['close'],
  data() {
    return {
      page: 0,
      size: 10,
      data: [],
      total: 0,
      loading: false
    }
  },
  watch: {
    dialogVisible(val) {
      if (val) {
        this.page = 0
        this.size = 10
        this.getData()
      }
    }
  },
  methods: {
    getData(resetPage = true) {
      this.loading = true
      this.page = resetPage ? 0 : this.page
      const params = {
        state: this.state,
        page: this.page,
        size: this.size
      }
      overviewApi.queryAllService(params).then(res => {
        this.data = res.content
        this.total = res.totalElements
      }
      ).finally(() => {
        this.loading = false
      })
    },
    transformState(state) {
      switch (state) {
        case 'GOOD':
          return '正常'
        case 'BAD':
          return '异常'
        case 'CONCERNING':
          return '告警'
        default:
          return '未知'
      }
    },
    handleCurrentChange(page) {
      this.page = page - 1
      this.getData(false)
    },
    handleSizeChange(size) {
      this.size = size
      this.getData(true)
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
